/**
 * NameValidator.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2013年10月21日
 */
package com.pshare.framework.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;

import com.pshare.constant.ReturnMsg;
import com.pshare.constant.SeaxConstants;
import com.pshare.framework.annotation.NameCheck;
import com.pshare.framework.annotation.PIntegerCheck;

/**
 * 正整数
 */
public class PIntegerValidator implements ConstraintValidator<PIntegerCheck, String> {

	private static String REGEX = "^[1-9]\\d*$";

	private static Pattern pattern = Pattern.compile(REGEX);

	@Override
	public void initialize(PIntegerCheck constraintAnnotation) {
		
	}

	@Override
	public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
		if (StringUtils.isBlank(object))
			return false;

		String input = object.toString().trim().replaceAll(SeaxConstants.BLACKSLASH + SeaxConstants.PLUS, SeaxConstants.BLANK);
		boolean valid = false;
		Matcher matcher = pattern.matcher(input);
		if (matcher.matches()) {
			valid = true;
		}
        return valid;
	}

}
